package com.ruoyi.controller.file;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.file.PFileInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州学院-计算机与信息工程学院
 * @ClassName PFileInfoController
 * @Description TODO
 * @Date 2022/07/02 16:07
 */
@RestController
@RequestMapping("/file/fileInfo")
public class PFileInfoController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PFileInfoService fileInfoService;

    /**
     *  上传文件
     * @param file 文件
     * @return 数据表中添加的id值
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("avatarfile") MultipartFile file) {

        long res = fileInfoService.uploadFile(file);
        if (res <= 0) {

            logger.error("上传失败！");
            return AjaxResult.error("上传失败！");
        }
        return AjaxResult.success(res);
    }
}
